package controls;

import java.util.Observable;
import java.util.Observer;

import views.PanFunEditarServico;
import views.WinFuncionario;

import models.ClienteLink;
import models.ServicoTNode;
import models.VeiculoTNode;

public class ControllerFunEditarServico implements Observer{
	private PanFunEditarServico pan_fun_editar_servico;
	private WinFuncionario winFuncionario;
	private ServicoTNode editedNode;
	private ClienteLink clienteLink;
	private VeiculoTNode veicTNode;

	public void setPanel(ServicoTNode servTNode, WinFuncionario windowFuncionario) {
		winFuncionario = windowFuncionario;
		editedNode = servTNode;
		veicTNode = (VeiculoTNode) ControllerLogin.getPersistence().find("VeiculoTree", servTNode.getVeiculoId());
		clienteLink = (ClienteLink) ControllerLogin.getPersistence().find("UsuarioLinkListInt", veicTNode.getClienteId());
		
		pan_fun_editar_servico = new PanFunEditarServico();
		pan_fun_editar_servico.criaPanel();
		pan_fun_editar_servico.addObserver(this);
		
		pan_fun_editar_servico.setServicoId(Integer.toString(servTNode.getServicoId()));
		pan_fun_editar_servico.setNomeCliente(clienteLink.getNome());
		pan_fun_editar_servico.setVeiculoId(String.valueOf(servTNode.getVeiculoId()));
		pan_fun_editar_servico.setData(servTNode.getData());
		pan_fun_editar_servico.setRelatorio(servTNode.getRelatorio());
		pan_fun_editar_servico.setValor(String.valueOf(servTNode.getValor()));
		
		winFuncionario.setContent(pan_fun_editar_servico.getPanel());
	}

	@Override
	public void update(Observable arg0, Object arg1) {
		String temp = new String((String) arg1);
		if(temp.compareTo("AddPeca")==0){
			
		}
		else{
			String[] data_form = new String[4];
			data_form = temp.split("&");
			editedNode.setModServico(Integer.parseInt(data_form[0]));
			editedNode.setEstServico(Integer.parseInt(data_form[1]));
			editedNode.setRelatorio(data_form[2]);
			editedNode.setGarantia(Integer.parseInt(data_form[3]));
			ControllerFunVerServico verServ = new ControllerFunVerServico();
			verServ.setPanel(editedNode, winFuncionario);
		}
	}
}
